<?php
//生产管理
namespace Admin\Controller;
use Think\Controller;
class ProductController extends CommonController {
	//流水单号
	public function ajax_orderno(){
		$t_no=serialNumber('ProductOrder');//流水号
		$order_no='PRO'.date('Ymd',time()).$t_no;//流水单号
		$return['status']=1;
		$return['order_no']=$order_no;
		$this->ajaxReturn($return);
	}
	//批次号
	public function ajax_batchno(){
		$batch_no='PRO'.date('YmdHis',time());//批次号
		$return['status']=1;
		$return['batch_no']=$batch_no;
		$this->ajaxReturn($return);
	}
	//产品编码
	public function ajax_goodsno(){
		$warehouse_code=session('warehouse_code');
		$goods_no=$warehouse_code.date('YmdHis',time());//产品编码
		$return['status']=1;
		$return['goods_no']=$goods_no;
		$this->ajaxReturn($return);
	}
	//产品料号
	public function ajax_sourceno(){
		$source_no=date('YmdHis',time());//产品料号
		$return['status']=1;
		$return['source_no']=$source_no;
		$this->ajaxReturn($return);
	}
	//副产品
	public function byproductorder(){
				// $uid=session('uid');//当前账号id
		// $user=M('AuthUser',C('DB_PRS'),C('DB_DEPOT'));//用户表
		// $userinfo=$user->where(array('uid'=>$uid))->find();
		
		$section_lists=sectionlist();//部门列表
		$unit_lists=unitlist();//单位列表
		$personnel_lists=personnellist();//员工列表
		$warehouse_lists=depot_list();//仓库列表
		
		$this->assign('section_lists',json_encode($section_lists));
		$this->assign('unit_lists',json_encode($unit_lists));
		$this->assign('personnel_lists',json_encode($personnel_lists));
		$this->assign('warehouse_lists',json_encode($warehouse_lists));
		
		$m=M('Personnel',C('DB_PRS'),C('DB_DEPOT'));//员工表
		$n=M('Unit',C('DB_PRS'),C('DB_DEPOT'));
		// $curr_personnel=$m->where(array('no'=>$userinfo['personnel_no']))->find();//当前用户信息
		
		$personnel_list=$m->where(array('status'=>1))->select();
		$unit_list=$n->select();
		$depotlist=depot_list();
		$section_list=sectionlist();//部门列表
		// $this->assign('curr_personnel',$curr_personnel);
		$this->assign('section_list',$section_list);
		$this->assign('personnel_list',$personnel_list);
		$this->assign('depotlist',$depotlist);
		$this->assign('unit_list',$unit_list);
		$this->display();
	}
	public function productorder(){
		// $uid=session('uid');//当前账号id
		// $user=M('AuthUser',C('DB_PRS'),C('DB_DEPOT'));//用户表
		// $userinfo=$user->where(array('uid'=>$uid))->find();
		
		$section_lists=sectionlist();//部门列表
		$unit_lists=unitlist();//单位列表
		$personnel_lists=personnellist();//员工列表
		$warehouse_lists=depot_list();//仓库列表
		
		$this->assign('section_lists',json_encode($section_lists));
		$this->assign('unit_lists',json_encode($unit_lists));
		$this->assign('personnel_lists',json_encode($personnel_lists));
		$this->assign('warehouse_lists',json_encode($warehouse_lists));
		
		$m=M('Personnel',C('DB_PRS'),C('DB_DEPOT'));//员工表
		$n=M('Unit',C('DB_PRS'),C('DB_DEPOT'));
		// $curr_personnel=$m->where(array('no'=>$userinfo['personnel_no']))->find();//当前用户信息
		
		$personnel_list=$m->where(array('status'=>1))->select();
		$unit_list=$n->select();
		$depotlist=depot_list();
		$section_list=sectionlist();//部门列表
		// $this->assign('curr_personnel',$curr_personnel);
		$this->assign('section_list',$section_list);
		$this->assign('personnel_list',$personnel_list);
		$this->assign('depotlist',$depotlist);
		$this->assign('unit_list',$unit_list);
		$this->display();
	}

	//领料单
	public function picklist(){
		$section_lists=sectionlist();//部门列表
		$unit_lists=unitlist();//单位列表
		$personnel_lists=personnellist();//员工列表
		$warehouse_lists=depot_list();//仓库列表
		
		$this->assign('section_lists',json_encode($section_lists));
		$this->assign('unit_lists',json_encode($unit_lists));
		$this->assign('personnel_lists',json_encode($personnel_lists));
		$this->assign('warehouse_lists',json_encode($warehouse_lists));

		$this->display();
	}
	//领料单详情
	public function picklistorderInfo(){
		$warehouse_lists=depot_list();//仓库列表
		$this->assign('depotlist',$warehouse_lists);
		$this->display();
	}


	//转换单详情
	public function changelistorderInfo(){
		$this->display();
	}
	//转换单
	public function changelist(){
		$section_lists=sectionlist();//部门列表
		$unit_lists=unitlist();//单位列表
		$personnel_lists=personnellist();//员工列表
		$warehouse_lists=depot_list();//仓库列表
		
		$this->assign('section_lists',json_encode($section_lists));
		$this->assign('unit_lists',json_encode($unit_lists));
		$this->assign('personnel_lists',json_encode($personnel_lists));
		$this->assign('warehouse_lists',json_encode($warehouse_lists));
		$this->display();
	}
	public function productorderInfo(){
		$this->display();
	}
	public function materialscrap(){
		$this->display();
	}
	public function confirm_editmaterialscrap(){
		$m=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
		$material_price=I('material_price');
		$scrap_price=I('scrap_price');
		$id=I('id');
        $map['id']=$id;
		$data['material_price']=$material_price;
		$data['scrap_price']=$scrap_price;
		
        $result=$m->where($map)->save($data);
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
	}

	//打印
	public function printproduct(){
			$id=I('id');
			$cid=I('cid');
			
			$ProductOrderDetail = M('ProductOrderDetail',C('DB_PRS'),C('DB_DEPOT'));
			$country_model = M('country',C('DB_PRS'),C('DB_DEPOT'));
			$ProductOrder=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
			$coin_model = M('coin',C('DB_PRS'),C('DB_DEPOT'));
			$Warehouse=M('Warehouse');
			
			$country = $country_model->select();
			
			$detail=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'))->where('id='.$id)->find();
			$detail['section_name']=M('Section',C('DB_PRS'),C('DB_DEPOT'))->where(array('no'=>$detail['section_no']))->find();//需求部门
			$detail['people_purchasing_name']=M('Personnel',C('DB_PRS'),C('DB_DEPOT'))->where(array('no'=>$detail['people_purchasing_no']))->find();//申请人
			$detail['people_make_name']=M('Personnel',C('DB_PRS'),C('DB_DEPOT'))->where(array('no'=>$detail['people_make_no']))->find();//制单人
			$detail['unit_name']=M('Unit',C('DB_PRS'),C('DB_DEPOT'))->where(array('unit_no'=>$detail['unit1']))->find();//主单位名称
			$inbound_warehouse_info=$Warehouse->where(array('warehouse_code'=>$detail['inbound_warehouse_code']))->find();
			$detail['inbount_warehouse_name']=$inbound_warehouse_info['name'];
			// dump($detail);
			// $listrows=10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['plan_id']=$id;
			$map['status']=array('neq',-1);
			$count=$ProductOrderDetail->where($map)->count();
			$list   = $ProductOrderDetail->where($map)->order('id desc')->select();
			
			// $Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			// $show    = $Page->show();// 分页显示输出
			$count_price=0;//合计总金额
			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['currency']))->find();
				
				$list[$k]['outbound_warehouse_name']=$Warehouse->where(array('warehouse_code'=>$v['outbound_warehouse_code']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
				
				$list[$k]['goods_info']=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'))->field('goods_no,goods_name,goods_spec,unit1,price,batch_no')->where(array('id'=>$v['goods_id']))->find();
				
				$list[$k]['goods_info']['unit_name']=M('Unit',C('DB_PRS'),C('DB_DEPOT'))->where(array('unit_no'=>$list[$k]['goods_info']['unit1']))->find();//商品主单位名称
				// echo M('Unit',C('DB_PRS'),C('DB_DEPOT'))->getlastsql();
				$count_price+=$list[$k]['goods_info']['price']*$v['weight'];
            }
			// dump($detail);
			// $this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);

			$this->assign("count_price",$count_price);
			$this->assign("country",$country);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
			if($cid==1){//生产计划单
				$this->display('print_examinedetail');
			}
			if($cid==2){//领料单
				$this->display('print_examinedetailpicking');
			}
			if($cid==3){//转换单
				$this->display('print_examinedetailchange');
			}
	}
	//生产计划单副产品列表
	public function get_byproductorder(){
		$m=M('ProductOrderByproduct',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
        //搜索条件
        $map=array();
        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		
		$status=I('status');
		if(empty($status)){
			$map['status']=array('neq',-1);
		}else{
			$map['status']=$status;
		}
        
        $count=$m->where($map)->count();
        if(empty($map)){
            $info=$m->order($sort.' '.$order)->select();
        }else{
            $info=$m->where($map)->order($sort.' '.$order)->select();
        }

		foreach($info as $k=>$v){
			// $count_goods=$n->where(array('plan_id'=>$v['id']))->count();
			// if(!$count_goods){//没有详情信息
				// $info[$k]['add_time']=date('Y-m-d',$v['add_time']);
			// }
			$info[$k]['add_time']=date('Y-m-d',$v['add_time']);
		}
		
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
        $this->ajaxReturn($data);
	}
	//生产计划单列表
	public function get_productorder(){
		
		$m=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('ProductOrderDetail',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
        //搜索条件
        $map=array();
        $purchasing_no=$ck->in('单号','purchasing_no','ennumstr','',0,0);       
        !empty($purchasing_no)?$map['purchasing_no']=array('like','%'.$purchasing_no.'%'):'';
	
        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);  
		
        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		$status=I('status');
		if(empty($status)){
			$map['status']=array('neq',-1);
		}else{
			$map['status']=$status;
		}
        
        $count=$m->where($map)->count();
        if(empty($map)){
            $info=$m->page($page,$rows)->order($sort.' '.$order)->select();
        }else{
            $info=$m->where($map)->page($page,$rows)->order($sort.' '.$order)->select();
        }

		foreach($info as $k=>$v){
			// $count_goods=$n->where(array('plan_id'=>$v['id']))->count();
			// if(!$count_goods){//没有详情信息
				// $info[$k]['add_time']=date('Y-m-d',$v['add_time']);
			// }
			$info[$k]['add_time']=date('Y-m-d',$v['add_time']);
			$info[$k]['examine_time']=date('Y-m-d',$v['examine_time']);
			
			if(!$v['status']){
				$info[$k]['color']=0;
			}
		}
		
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
        $this->ajaxReturn($data);
	}
	//领料单列表
	public function get_picklistorder(){
		$m=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
        //搜索条件
        $map=array();
        $picklist_order_no=$ck->in('单号','picklist_order_no','ennumstr','',0,0);       
        !empty($picklist_order_no)?$map['picklist_order_no']=array('like','%'.$picklist_order_no.'%'):'';
	
        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);  
		
        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		$status=1;
		if(empty($status)){
			$map['status']=array('neq',-1);
		}else{
			$map['status']=$status;
		}
        
        $count=$m->where($map)->count();
        if(empty($map)){
            $info=$m->page($page,$rows)->order($sort.' '.$order)->select();
        }else{
            $info=$m->where($map)->page($page,$rows)->order($sort.' '.$order)->select();
        }

		foreach($info as $k=>$v){
			$info[$k]['add_time']=date('Y-m-d',$v['add_time']);
			$info[$k]['picklist_examine_time']=date('Y-m-d',$v['picklist_examine_time']);
			if(!$v['status']){
				$info[$k]['color']=0;
			}
		}
		
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
        $this->ajaxReturn($data);
	}
	//转换单列表
	public function get_changelistorder(){
		$m=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
        //搜索条件
        $map=array();
        $changelist_order_no=$ck->in('单号','changelist_order_no','ennumstr','',0,0);       
        !empty($changelist_order_no)?$map['changelist_order_no']=array('like','%'.$changelist_order_no.'%'):'';
	
        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);  
		
        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		$status=1;
		if(empty($status)){
			$map['status']=array('neq',-1);
		}else{
			$map['status']=$status;
		}
        $map['picklist_examined_status']=1;
        $count=$m->where($map)->count();
        if(empty($map)){
            $info=$m->page($page,$rows)->order($sort.' '.$order)->select();
        }else{
            $info=$m->where($map)->page($page,$rows)->order($sort.' '.$order)->select();
        }

		foreach($info as $k=>$v){
			$info[$k]['add_time']=date('Y-m-d',$v['add_time']);
			$info[$k]['changelist_examine_time']=date('Y-m-d',$v['changelist_examine_time']);
			if(!$v['status']){
				$info[$k]['color']=0;
			}
		}
		
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
        $this->ajaxReturn($data);
	}
	//详情列表
	public function get_productorderInfo(){
		$m=M('ProductOrderDetail',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
        //搜索条件
        $map=array();
        $plate_number=$ck->in('订单编码','plate_number','ennumstr','',0,0); 
		$id=I('id');
		$goods_cate=I('goods_cate');
		if(!empty($goods_cate)){
			$map['m.goods_cate']=$goods_cate;
		}
        !empty($plate_number)?$map['m.plate_number']=array('like','%'.$plate_number.'%'):'';
		$map['m.plan_id']=$id;
        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);  
		
        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		$status=I('status');
		if(empty($status)){
			$map['m.status']=array('neq',-1);
		}else{
			$map['m.status']=$status;
		}
		
        // $count=$m->alias('m')->join('__GOODS__ as g on m.goods_id=g.id')->where($map)->count();
        // $info=$m->alias('m')->join('__GOODS__ as g on m.goods_id=g.id')->field('m.id,m.plan_id,m.goods_id,m.scrap_weight,m.material_weight,m.outbound_warehouse_code,m.batch_no,m.mark,g.goods_no,g.goods_name,g.source_no,g.goods_spec,g.current_type,g.unit1,g.goods_price,m.weight,m.add_time,m.status')->where($map)->page($page,$rows)->order($sort.' '.$order)->select();
		
		$count=$m->alias('m')->join('__ARRIVAL_ORDER__ as n on m.goods_id=n.id')->where($map)->count();
        $info=$m->alias('m')->join('__ARRIVAL_ORDER__ as n on m.goods_id=n.id')->field('m.id,m.plan_id,m.goods_id,m.scrap_weight,m.material_weight,m.outbound_warehouse_code,m.batch_no,m.mark,n.goods_no,n.manual_id as manua_id,n.source_no,n.goods_name,n.goods_spec,n.currency as current_type,n.country_code,n.unit1,n.price,n.batch_no,m.weight,m.add_time,m.status')->where($map)->page($page,$rows)->order($sort.' '.$order)->select();
		
        // echo $m->getlastsql();
		// die;
		foreach($info as $k=>$v){
			$info[$k]['add_time']=date('Y-m-d',$v['add_time']);
			if(!$v['status']){
				$info[$k]['color']=0;
			}
		}
		
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
		
        $this->ajaxReturn($data);
	}
	//添加详情
    public function productaddgoods(){
		$m=M('ProductOrderDetail',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput');  
        $id=$ck->in('id','id','string','',1,0);
		$plan_id=$ck->in('订单id','plan_id','intval','',1,0);
		//检查是否已审核（审核后不能再添加）
		$order_info=$n->where(array('id'=>$plan_id))->find();
		if($order_info['status']==1){
			$return['status']=0;
			$return['message']='已通过审核不能再添加！';
			$this->ajaxReturn($return);
		}
        if(empty($id)) exit;//为空
		$data_n['add_goods']=1;
		$data_m['plan_id']=$plan_id;
		$data_m['goods_cate']=2;
		$arr=explode(',', $id);
		$i=0;//新增
		$j=0;//失败
		$k=0;//重复
		foreach($arr as $key=>$v){
			$data_m['goods_id']=$v;
			$detail=$m->where($data_m)->find();
			// echo $m->getlastsql();
			if($detail){
				$k++;
			}else{
				$result=$m->add($data_m);
				// echo $m->getlastsql();
				// die;
				if($result){
					$i++;
				}else{
					$j++;
				}
			}
		}
		if($i){
			$n->where(array('id'=>$plan_id))->save($data_n);
			$message=$i.'个添加成功!';
		}
		if($j){
			$message=$j.'个添加失败!';
		}
		if($k){
			$message=$k.'个重复!';
		}
		$return['message']=$message;
        $return['status']=true;
        $this->ajaxReturn($return);
	}
	//添加辅料
    public function productaddgoodsF(){
		$m=M('ProductOrderDetail',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput');  
        $id=$ck->in('id','id','string','',1,0);
		$plan_id=$ck->in('订单id','plan_id','intval','',1,0);
		//检查是否已审核（审核后不能再添加）
		$order_info=$n->where(array('id'=>$plan_id))->find();
		if($order_info['status']==1){
			$return['status']=0;
			$return['message']='已通过审核不能再添加！';
			$this->ajaxReturn($return);
		}
        if(empty($id)) exit;//为空
		$data_n['add_goods']=1;
		$data_m['plan_id']=$plan_id;
		$data_m['goods_cate']=3;
		$arr=explode(',', $id);
		$i=0;//新增
		$j=0;//失败
		$k=0;//重复
		foreach($arr as $key=>$v){
			$data_m['goods_id']=$v;
			$detail=$m->where($data_m)->find();
			// echo $m->getlastsql();
			if($detail){
				$k++;
			}else{
				$result=$m->add($data_m);
				// echo $m->getlastsql();
				// die;
				if($result){
					$i++;
				}else{
					$j++;
				}
			}
		}
		if($i){
			$n->where(array('id'=>$plan_id))->save($data_n);
			$message=$i.'个添加成功!';
		}
		if($j){
			$message=$j.'个添加失败!';
		}
		if($k){
			$message=$k.'个重复!';
		}
		$return['message']=$message;
        $return['status']=true;
        $this->ajaxReturn($return);
	}
	//编辑领料
	public function productordergoodsSave(){
		$m=M('ProductOrderDetail',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
		// dump(I());
		
		$weight=I('weight');
		$outbound_warehouse_code=I('outbound_warehouse_code');
		$mark=I('mark');
		$batch_no=I('batch_no');
		$id=I('id');
        $map['id']=$id;
		$data['weight']=$weight;
		$data['outbound_warehouse_code']=$outbound_warehouse_code;
		$data['mark']=$mark;
		$data['batch_no']=$batch_no;
		//检查是否已审核（审核后不能再添加）
		$detail_info=$m->find($id);
		$order_info=$n->where(array('id'=>$detail_info['plan_id']))->find();
		if($order_info['status']<>1){
			$return['status']=0;
			$return['message']='未审核通过不能修改!';
			$this->ajaxReturn($return);
		}
		$ao_info=$ao->where(array('id'=>$detail_info['goods_id']))->find();
		$storage_nums=$ao_info['storage_amount'];//库存数量
		if($data['weight']>$storage_nums){
			$return['message']='此数量大于库存数量不能保存';
            $return['status']=false;
            $this->ajaxReturn($return);
		}
        $result=$m->where($map)->save($data);
		// echo $m->getlastsql();
		// die;
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
	}
	//编辑损耗。边角料
	public function productordergoodsMSave(){
		$m=M('ProductOrderDetail',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
		$g=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
		// dump(I());
		
		$material_weight=I('material_weight');
		$scrap_weight=I('scrap_weight');
		$id=I('id');
        $map['id']=$id;
		$data['material_weight']=$material_weight;
		$data['scrap_weight']=$scrap_weight;
		//检查是否已领料
		$detail_info=$m->where($map)->find();
		// $order_info=$n->where(array('id'=>$detail_info['plan_id']))->find();
		
		if($detail_info['weight']==0){
			$return['status']=0;
			$return['message']='未领料不能修改!';
			$this->ajaxReturn($return);
		}
		//检查是否已设置损耗/边角料价格
		$ao_info=$ao->where(array('id'=>$detail_info['goods_id']))->find();
		$goods_info=$g->where(array('goods_no'=>$ao_info['goods_no'],'manua_id'=>$ao_info['manual_id']))->find();
		
		if(empty($goods_info['material_price']) or $goods_info['material_price']==0){
			$return['status']=0;
			$return['message']='未设置此原料损耗价格不能修改!';
			$this->ajaxReturn($return);
		}
		if(empty($goods_info['scrap_price']) or $goods_info['scrap_price']==0){
			$return['status']=0;
			$return['message']='未设置此原料边角料价格不能修改!';
			$this->ajaxReturn($return);
		}
		//判断损耗/边角料重量之和
		$count_weight=$material_weight+$scrap_weight;
		if($count_weight>$detail_info['weight']){
			$return['status']=0;
			$return['message']='损耗与边角料的重量不能大于领料的重量!';
			$this->ajaxReturn($return);
		}
        $result=$m->where($map)->save($data);
		// echo $m->getlastsql();
		// die;
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
	}
	//编辑辅料
	public function productordergoodsFSave(){
		$m=M('ProductOrderDetail',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
		// dump(I());
		$weight=I('weight');
		$id=I('id');
        $map['id']=$id;
		$data['weight']=$weight;
		$outbound_warehouse_code=I('outbound_warehouse_code');
		$mark=I('mark');
		$batch_no=I('batch_no');
		$data['outbound_warehouse_code']=$outbound_warehouse_code;
		$data['mark']=$mark;
		$data['batch_no']=$batch_no;
		
		//检查是否已审核（审核后不能再添加）
		$detail_info=$m->find($id);
		$order_info=$n->where(array('id'=>$detail_info['plan_id']))->find();
		if($order_info['status']<>1){
			$return['status']=0;
			$return['message']='未审核通过不能修改!';
			$this->ajaxReturn($return);
		}
		$ao_info=$ao->where(array('id'=>$detail_info['goods_id']))->find();
		$storage_nums=$ao_info['storage_amount'];//库存数量
		if($data['weight']>$storage_nums){
			$return['message']='此数量大于库存数量不能保存';
            $return['status']=false;
            $this->ajaxReturn($return);
		}
        $result=$m->where($map)->save($data);
		// echo $m->getlastsql();
		// die;
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
	}
	//删除
	public function productordergoodsDel(){
		$return=tableDel('ProductOrderDetail');
        $this->ajaxReturn($return);
	}
	//清空
    public function productordergoodsClear(){
		$return=tableClear('ProductOrderDetail');
        $this->ajaxReturn($return);
    }
	//恢复
    public function productordergoodsUndo(){
		$return=tableUndo('ProductOrderDetail');
        $this->ajaxReturn($return);
    }
	//添加副产品 
    public function byproductorderAdd(){
		$m=M('ProductOrderByproduct',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['pro_name']=I('pro_name');
		$data['pro_spec']=I('pro_spec');
		$data['source_no']=I('source_no');
		$data['pro_no']=I('pro_no');
		
		$data['pro_num_plan']=I('pro_num_plan');
		$data['unit1']=I('unit1');
		$data['inbound_warehouse_code']=I('inbound_warehouse_code');
		
		$data['product_weeks']=I('product_weeks');
		$data['product_marks']=I('product_marks');
		
		$data['add_time']=time();
		
        $result=$m->add($data);
        if(false===$result){
            $return['message']='添加失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	
	//更新副产品
    public function byproductorderSave(){
		$m=M('ProductOrderByproduct',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['pro_name']=I('pro_name');
		$data['pro_spec']=I('pro_spec');
		$data['source_no']=I('source_no');
		$data['pro_no']=I('pro_no');

		$data['pro_num_plan']=I('pro_num_plan');
		$data['unit1']=I('unit1');
		$data['inbound_warehouse_code']=I('inbound_warehouse_code');
		
		$data['product_weeks']=I('product_weeks');
		$data['product_marks']=I('product_marks');
		
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
		// echo $m->getlastsql();
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//添加
    public function productorderAdd(){
		$m=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['purchasing_no']=I('purchasing_no');
		$data['section_no']=I('section_no');
		$data['pro_name']=I('pro_name');
		$data['pro_spec']=I('pro_spec');
		$data['source_no']=I('source_no');
		$data['pro_no']=I('pro_no');
		$data['batch_no']=I('batch_no');
		$data['people_make_no']=I('people_make_no');
		$data['people_purchasing_no']=I('people_purchasing_no');
		
		$data['pro_num_plan']=I('pro_num_plan');
		$data['unit1']=I('unit1');
		$data['inbound_warehouse_code']=I('inbound_warehouse_code');
		$data['mark']=I('mark');
		
		$data['product_weeks']=I('product_weeks');
		$data['product_marks']=I('product_marks');
		
		$data['add_time']=time();
		
        $result=$m->add($data);
        if(false===$result){
            $return['message']='添加失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	
	//更新
    public function productorderSave(){
		$m=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['purchasing_no']=I('purchasing_no');
		$data['section_no']=I('section_no');
		$data['pro_name']=I('pro_name');
		$data['pro_spec']=I('pro_spec');
		$data['source_no']=I('source_no');
		$data['pro_no']=I('pro_no');
		$data['batch_no']=I('batch_no');
		$data['people_make_no']=I('people_make_no');
		$data['people_purchasing_no']=I('people_purchasing_no');
		
		$data['pro_num_plan']=I('pro_num_plan');
		$data['unit1']=I('unit1');
		$data['inbound_warehouse_code']=I('inbound_warehouse_code');
		$data['mark']=I('mark');
		
		$data['product_weeks']=I('product_weeks');
		$data['product_marks']=I('product_marks');
		
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
		// echo $m->getlastsql();
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//转换单更新
    public function productorderNumsSave(){
		$m=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['pro_num']=I('pro_num');//完成数量
		$data['inbound_num']=I('inbound_num');
		$data['conforming_p_num']=I('conforming_p_num');
		$data['nonconforming_p_num']=I('nonconforming_p_num');
		$data['scrap_one_num']=I('scrap_one_num');
		$data['inbound_price']=I('inbound_price');
		$data['sell_price']=I('sell_price');
		$data['factory_price']=I('factory_price');
		$data['bar_code']=I('bar_code');
		$data['manua_id']=I('manua_id');
		// $data['pro_num']=I('pro_num');
		if($data['pro_num']<$data['inbound_num']){
			$return['message']='完成数量不能小于实际入库数量';
            $return['status']=2;
            $this->ajaxReturn($return);
		}
		if($data['inbound_num']>$data['conforming_p_num']+$data['nonconforming_p_num']+$data['scrap_one_num']){
			$return['message']='实际入库数量不能大于[合格品数量+残次品数量+报废品数量]';
            $return['status']=2;
            $this->ajaxReturn($return);
		}
		if($data['inbound_price']==0 or $data['sell_price']==0 or $data['factory_price']==0 ){
			$return['message']='所有价格不能为0';
            $return['status']=2;
            $this->ajaxReturn($return);
		}
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	
	//审核生产单
    public function examineProductorder(){
		//条件
		$mm=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
		$mmd=M('ProductOrderDetail',C('DB_PRS'),C('DB_DEPOT'));
		$id=I('id');
		$value=I('value');
        if(empty($id)) exit;//为空
		$arr=explode(',', $id);
		
		foreach($arr as $keys=>$values){
			$order_info=$mm->where(array('id'=>$values))->find();
			$count=$mmd->where(array('plan_id'=>$values,'goods_cate'=>2))->count();//原料数量
			if(!$count){
				$return['status']=0;
				$return['message']='单号:'.$order_info['purchasing_no'].'，未添加原料不能审核！';
				$this->ajaxReturn($return);
			}
			$map['id']=$values;
			if($value==1){
				$data['status']=1;
				//审核通过则生成领料单号
				$data['picklist_order_no']='P'.$order_info['purchasing_no'];
			}else if($value==2){
				$data['status']=2;
			}else{
				$return['message']='审核状态参数错误!';
				$return['status']=false;
				$this->ajaxReturn($return);
				exit;
			}
			$data['examine_time']=time();
			
			$result=$mm->where($map)->save($data);
		}
		
		// echo $m->getlastsql();
		// die;
        if(!$result){
            $return['message']='审核失败!';
            $return['status']=false;  
        }else{
             $return['message']='审核成功!';
             $return['status']=true; 
        }        
       // return $return;
		// $return=tableExamine('ProductOrder');
        $this->ajaxReturn($return);
    }
	//审核领料单
    public function examinePicklistorder(){
		$m=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
		$md=M('ProductOrderDetail',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$so=M('StorageOutbound',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;  
        $id=I('id');
		$value_s=I('value');
        if(empty($id)) exit;//为空
        $arr=explode(',', $id);
		foreach($arr as $k=>$v){
			$maps['plan_id']=$v;
			$map_order['id']=$v;
			$info_order=$m->where($map_order)->find();
			if($info_order['picklist_examined_status']==1){
				$return['message']='已审核通过，不能再次审核!';
				$return['status']=false; 
				$this->ajaxReturn($return);
			}
			$info=$md->where($maps)->select();
			if(!$info){
				$return['message']='没有详情信息，请添加详情!';
				$return['status']=false; 
				$this->ajaxReturn($return);
			}else{
				foreach($info as $key=>$value){
					if(empty($value['weight']) or $value['weight']==0){
						$return['message']='主单位数量不能为空或0!';
						$return['status']=false; 
						$this->ajaxReturn($return);
					}else{
						if($value_s==1){//通过则冻结库存
							//冻结要其它出库数量
							// $data_ao['freez_amount']=array('exp','freez_amount+'.$value['weight']);
							$data_ao['storage_amount']=array('exp','storage_amount-'.$value['weight']);
							$ao->where(array('id'=>$value['goods_id']))->save($data_ao);//更新库存并冻结要出库数量
							$so->add(array('outbound_order_id'=>$info['id'],'inbound_order_id'=>$vs['goods_id'],'nums'=>$vs['weight'],'add_time'=>time(),'out_cate'=>5));//更新出库记录out_cate:(出库类型1:订单出库2:异常出库3:销售单出库数量4:调拨出库5:领料出库6:其它出库)
							// echo $ao->getlastsql();
							// die;
						}
					}
				}
			}
		}
		
		foreach($arr as $keys=>$values){
			$order_info=$m->where(array('id'=>$values))->find();
			$map['id']=$values;
			if($value_s==1){
				$data['picklist_examined_status']=1;
				//审核通过则生成转换单号
				$data['changelist_order_no']='C'.$order_info['purchasing_no'];
			}else if($value_s==2){
				$data['picklist_examined_status']=2;
			}else{
				$return['message']='审核状态参数错误!';
				$return['status']=false;
				$this->ajaxReturn($return);
				exit;
			}
			$data['picklist_examine_time']=time();
			
			$result=$m->where($map)->save($data);
		}
		// echo $m->getlastsql();
		// die;
        if(!$result){
            $return['message']='审核失败!';
            $return['status']=false;  
        }else{
             $return['message']='审核成功!';
             $return['status']=true; 
        }        
        // return $return;
		// $return=tableExamine('ProductOrder');
        $this->ajaxReturn($return);
    }
	//审核转换单
    public function examineChangelistorder(){
		$m=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;  
        $id=I('id');
		$value=I('value');
        if(empty($id)) exit;//为空
        $arr=explode(',', $id);
        $map['id']=array('in',$arr);
		if($value==1){
			$data['changelist_examined_status']=1;
		}else if($value==2){
			$data['changelist_examined_status']=2;
		}else{
			$return['message']='审核状态参数错误!';
            $return['status']=false;
			$this->ajaxReturn($return);
			exit;
		}
		$data['changelist_examine_time']=time();
		
        $result=$m->where($map)->save($data);
		// echo $m->getlastsql();
		// die;
        if(!$result){
            $return['message']='审核失败!';
            $return['status']=false;  
        }else{
             $return['message']='审核成功!';
             $return['status']=true; 
        }        
       // return $return;
		// $return=tableExamine('ProductOrder');
        $this->ajaxReturn($return);
    }
	//删除
    public function byproductorderDel(){
		$return=tableDel('ProductOrderByproduct');
        $this->ajaxReturn($return);
    }
	//清空回收站
    public function byproductorderClear(){
		$return=tableClear('ProductOrderByproduct');
        $this->ajaxReturn($return);
    }
	//恢复回收站数据
    public function byproductorderUndo(){
		$return=tableUndo('ProductOrderByproduct');
        $this->ajaxReturn($return);
    }
	//删除
    public function productorderDel(){
		$return=tableDel('ProductOrder');
        $this->ajaxReturn($return);
    }
	//清空回收站
    public function productorderClear(){
		$return=tableClear('ProductOrder');
        $this->ajaxReturn($return);
    }
	//恢复回收站数据
    public function productorderUndo(){
		$return=tableUndo('ProductOrder');
        $this->ajaxReturn($return);
    }
	//成品入库
    public function product_inbound(){
		$m=M('ProductOrder',C('DB_PRS'),C('DB_DEPOT'));
		// $m=M('ProductOrderDetail',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput');
		$id=$ck->in('计划单id','id','string','',1,0);
        if(empty($id)) exit;//为空
        $arr=explode(',', $id);
		$map['id']=array('in',$arr);
		$plan_list=$m->where($map)->select();
		foreach($plan_list as $key=>$vs){
			if($vs['changelist_examined_status']<>1){
				$this->error('未审核通过不能入库！');
			}
		}
		// dump($plan_list);
		foreach($plan_list as $ks=>$v){
			if($v['inbound_status']==1){
					continue;//已经入库，则继续循环
			}
			// $arrival_goods=$m->where(array('plan_id'=>$v['id']))->select();
			// foreach($arrival_goods as $k=>$v){
				$receive_amount=$v['inbound_num'];//实收数量
				$conforming_p=$v['conforming_p_num'];//合格品数量
				$nonconforming_p=$v['nonconforming_p_num'];//残次品数量
				$scrap_one=$v['scrap_one_num'];//报废品数量
				//where唯一条件:商品编码相同，账册编码相同
				$where['goods_no']=$v['pro_no'];
				$where['manua_id']=$v['manua_id'];
				$res=$n->where($where)->find();
				if($res){//库存中已经有此商品，则更新库存数据
					$data['amount']=array('exp',"amount+$receive_amount");
					$data['conforming_p']=array('exp',"conforming_p+$conforming_p");
					$data['nonconforming_p']=array('exp',"nonconforming_p+$nonconforming_p");
					$data['scrap_one']=array('exp',"scrap_one+$scrap_one");
					//价格(平均价)=总价/总数量
					//(库存商品单价*库存数量+入库数量*入库单价)/(库存数量+入库数量)
					$data['goods_price']=($res['goods_price']*$res['amount']+$v['inbound_num']*$v['inbound_price'])/($res['amount']+$v['inbound_num']);
					$ress=$n->where($where)->save($data);
					// echo $n->getlastsql();
					if($ress!==false){
						$m->where(array("id"=>$v['id']))->setField('inbound_status',1);//更新入库状态
						//更新入库详情表
						
						$t_no=serialNumber('ArrivalOrder');//流水号
						$order_no='AO'.date('Ymd',time()).$t_no;//流水单号
						$batch_no='BAO'.date('YmdHis',time());//批次号
						
						$ao_data['order_no']=$order_no;
						$ao_data['batch_no']=$batch_no;
						$ao_data['old_purchasing_no']=$v['changelist_order_no'];
						$ao_data['old_goods_id']=$v['id'];//成品表对应id
						$ao_data['manual_id']=$v['manua_id'];
						// $ao_data['erp_code']=$value['list']['erp_code'];
						$ao_data['goods_no']=$v['pro_no'];
						$ao_data['goods_name']=$v['pro_name'];
						$ao_data['amount']=$v['inbound_num'];//入库数量
						$ao_data['receive_amount']=$v['inbound_num'];//入库数量
						$ao_data['storage_amount']=$v['inbound_num'];//库存数量
						$ao_data['declare_amount']=$v['inbound_num'];//入库数量
						$ao_data['unit1']=$v['unit1'];
						$data['bar_code']=$v['bar_code'];
						
						$ao_data['conforming_p']=$v['conforming_p_num'];
						$ao_data['nonconforming_p']=$v['nonconforming_p_num'];
						$ao_data['scrap_one']=$v['scrap_one_num'];
						
						$ao_data['unit1']=$v['unit1'];
						$ao_data['inbound_status']=1;//已入库
						$ao_data['status']=1;//已审核
						
						$status=$ao->add($ao_data);//到货通知单添加
						if(!$status){
							$this->error('更新入库详情失败');
						}
						continue;
					}else{
						// die;
						$this->error($v['pro_name'].'| 入库失败！');
					}
				}else{//库存中没有此商品,则添加此商品数据入库存
					$data['source_no']=$v['source_no'];
					$data['goods_name']=$v['pro_name'];
					$data['goods_no']=$v['pro_no'];
					$data['bar_code']=$v['bar_code'];
					$data['amount']=$receive_amount;
					$data['conforming_p']=$v['conforming_p_num'];
					$data['nonconforming_p']=$v['nonconforming_p_num'];
					$data['scrap_one']=$v['scrap_one_num'];
					$data['goods_price']=$v['inbound_price'];

					$data['manua_id']=$v['manua_id'];
					// $data['item_no']=$v['item_no'];
					// $data['item_type']=$v['item_type'];
					$data['goods_spec']=$v['pro_spec'];
					// $data['current_type']=$v['current'];
					// $data['declare_unit']=$v['declare_unit'];
					$data['unit1']=$v['unit1'];
					// $data['unit2']=$v['unit2'];
					
					$resss=$n->add($data);
					// echo $n->getlastsql();
					// die;
					if($resss){
						$m->where(array("id"=>$v['id']))->setField('inbound_status',1);//更新入库状态
						//更新入库详情表
						
						$t_no=serialNumber('ArrivalOrder');//流水号
						$order_no='AO'.date('Ymd',time()).$t_no;//流水单号
						$batch_no='BAO'.date('YmdHis',time());//批次号
						
						$ao_data['order_no']=$order_no;
						$ao_data['batch_no']=$batch_no;
						$ao_data['old_purchasing_no']=$v['changelist_order_no'];
						$ao_data['old_goods_id']=$v['id'];//成品表对应id
						$ao_data['manual_id']=$v['manua_id'];
						// $ao_data['erp_code']=$value['list']['erp_code'];
						$ao_data['goods_no']=$v['pro_no'];
						$ao_data['goods_name']=$v['pro_name'];
						$ao_data['amount']=$v['inbound_num'];//入库数量
						$ao_data['receive_amount']=$v['inbound_num'];//入库数量
						$ao_data['storage_amount']=$v['inbound_num'];//库存数量
						$ao_data['declare_amount']=$v['inbound_num'];//入库数量
						$ao_data['unit1']=$v['unit1'];
						$ao_data['inbound_status']=1;//已入库
						$ao_data['status']=1;//已审核
						$ao_data['price']=$v['inbound_price'];
						$ao_data['goods_spec']=$v['pro_spec'];
						$ao_data['bar_code']=$v['bar_code'];
						
						$ao_data['conforming_p']=$v['conforming_p_num'];
						$ao_data['nonconforming_p']=$v['nonconforming_p_num'];
						$ao_data['scrap_one']=$v['scrap_one_num'];
						
						$status=$ao->add($ao_data);//到货通知单添加
						if(!$status){
							$this->error('更新入库详情失败');
						}
						continue;
					}else{
						// die;
						$this->error($v['pro_name'].'| 入库失败！');
					}
				}
			$s->where(array("id"=>$v['id']))->setField('inbound_status',1);//更新入库状态
			// echo $s->getlastsql();
		}
		
		
        $return['info']='入库成功!';
        $return['status']=true;    
        $this->ajaxReturn($return);
    }
}